﻿create function [Base64].[Decode] ( @p_String varchar(max) )
returns varchar(max) as 
begin

  declare @returnString varchar(max);

  select @returnString = Convert( varchar(max), Convert( Xml, '' ).value( 'xs:base64Binary(sql:column("BinaryData"))', 'varbinary(max)' ) ) 
    from ( select Convert( varchar(max), @p_String ) As BinaryData) AS tmp;

  return @returnString;

end